package m.html.form;

import m.html.element.Element;
import m.html.element.Text;
import m.html.element.attr.Size;
import m.html.element.other.IElement;
import m.system.util.StringUtil;

public abstract class AbstractFormItem<T> {
    protected abstract IElement getItemElement();
    protected IElement getElement(){
        Element ele;
        String sv=StringUtil.isSpace(spanVal)?String.valueOf(span):spanVal;
        if(FormMode.ROW==this.mode){
            String cls="index-form-item";
            if(this.titleSize==Size.SMALL) cls="index-form-item-sm";
            if(this.titleSize==Size.LARGE) cls="index-form-item-lg";
            if(this.titleSize==Size.NONE) cls="index-form-item-no";
            ele=Element.tag("a-col").A(":span", 24/col+"*("+sv+")")
            .E(
                Element.tag("a-form-item").A("class",cls)
                .E(
                    Element.tag("template").A("#label")
                        .E(this.required?new Text("<span class=\"text-red text-sm\">✽</span>"):null).E(this.title)
                )
                .E(getItemElement())

            );
        }else{//normal  desc
            ele=Element.tag("a-descriptions-item").A(":span",sv).A(":contentStyle","{width:'"+(15+(span-1)*20)+"%'}");
            if(!StringUtil.isSpace(this.title.toString()))
                ele.E(Element.tag("template").A("#label")
                    .E(this.required?new Text("<span class=\"text-red text-sm\">✽</span>"):null).E(this.title));
            else
                ele.A(":labelStyle","{display:'none'}");
            ele.E(getItemElement());
        }
        return ele;
    }
    protected IElement title=new Text("");
    protected Size titleSize=Size.DEFAULT;
    protected int col=3;//列数
    protected String spanVal;//列数
    protected int span=1;//占列
    protected FormMode mode;//模式
    protected boolean required;//必填

    /**
     * 设置标题
     */
    public T setTitle(IElement title){
        this.title=title;
        return (T) this;
    }

    /**
     * 设置标题尺寸 ROW样式才生效
     */
    public T setTitleSize(Size size){
        this.titleSize=size;
        return (T) this;
    }

    /**
     * 设置占列
     */
    public T setSpan(int span){
        this.span=span;
        return (T) this;
    }
    /**
     * 设置占列表达式
     */
    public T setSpanVal(String spanVal){
        this.spanVal=spanVal;
        return (T) this;
    }

    /**
     * 设置必填
     */
    public T setRequired(boolean required){
        this.required=required;
        return (T) this;
    }
}
